package com.njxxted.activiti.modules.process.model.controller;

import com.google.common.collect.Maps;
import com.njxxted.activiti.common.response.ResponseVo;
import com.njxxted.activiti.common.util.BpmnBuilderUtil;
import com.njxxted.activiti.common.util.FeignUtil;
import com.njxxted.activiti.modules.api.CommonApi;
import com.njxxted.activiti.modules.process.cmd.BpmnModelCmd;
import com.njxxted.activiti.modules.process.model.service.IActModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.repository.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 描述：流程引擎模型 Controller
 * <p>
 * 作者：Ostrich Hu
 * 时间：2019/8/30 16:45 星期五
 */
@Api(tags = "流程模型")
@RestController
@RequestMapping(value = "/act/rest/model")
public class ActModelController {

    private final IActModelService iActModelService;

    public ActModelController(IActModelService iActModelService) {
        this.iActModelService = iActModelService;
    }

    @ApiOperation(value = "创建空模型(开发用)")
    @GetMapping("/create/dev")
    public void createModel1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BpmnModelCmd bpmnModelCmd = BpmnBuilderUtil.buildBpmnCmd(request);
        Model model = iActModelService.createBpmnModel(bpmnModelCmd);
        response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + model.getId());
    }

    @GetMapping("/create")
    @ApiOperation(value = "创建空模型")
    public ResponseVo createModel(HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        BpmnModelCmd bpmnModelCmd = BpmnBuilderUtil.buildBpmnCmd(request);
        Model model = iActModelService.createBpmnModel(bpmnModelCmd);
        map.put("data", model.getId());
        return ResponseVo.ok(map);
    }

    @ApiOperation(value = "查询模型")
    @GetMapping("/")
    public ResponseVo getModel(HttpServletRequest request) {
        return iActModelService.getModel(request);
    }

    @ApiOperation(value = "查询模型（用于发起流程弹窗）")
    @GetMapping("/{type}")
    public ResponseVo getModelForStartInstance(@PathVariable String type) {
        return iActModelService.getModelForStartInstance(type);
    }

    @ApiOperation(value = "查询模型分类")
    @GetMapping("/type")
    public ResponseVo getModelType(String type) {
        return new FeignUtil<CommonApi>().invokeCommonService(CommonApi.class).getKVList(type);
    }

    @ApiOperation(value = "删除模型")
    @DeleteMapping("/{modelId}/{isEmpty}")
    public ResponseVo deleteModel(@PathVariable String modelId, @PathVariable String isEmpty) {
        boolean flag = iActModelService.deleteModel(modelId, isEmpty);
        return flag ? ResponseVo.ok("模型[" + modelId + "]删除成功") : ResponseVo.error("模型[" + modelId + "]删除失败");
    }

    @ApiOperation(value = "根据模型id部署模型")
    @PostMapping("/deploy/{modelId}")
    public ResponseVo deploymentModel(@PathVariable String modelId) {
        boolean flag = iActModelService.deploymentModel(modelId);
        return flag ? ResponseVo.ok("模型[" + modelId + "]部署成功") : ResponseVo.error("模型[" + modelId + "]部署失败");
    }

    @GetMapping("/flowNode/{key}")
    @ApiOperation(value = "查询流程的所有流程节点", notes = "根据模型部署Id获取所有流程节点信息")
    public ResponseVo getFlowNode(@PathVariable String key) {
        return iActModelService.getFlowNode(key);
    }
}
